<?php
namespace Modules\Skinov\Controllers;

class UserController extends ControllerBase {
  public function indexAction() {
    $user = $this -> session -> get('user');
    if(!$user) { return $this -> returnFailByJson(['msg'=>'用户信息获取失败!']); }

    $res = (new \Phalcon\Mvc\Model\Query\Builder())
      -> from([
        'user' => 'Modules\Skinov\Models\UserModel',
        'user_wachat' => 'Modules\Skinov\Models\UserWechatModel'
      ])
      -> where('user.id = ' . $user['id'])
      -> andWhere('user.id = user_wachat.id')
      -> getQuery() -> getSingleResult();

      if($res -> user -> pid > 0) {
        $resPUser = (new \Modules\Skinov\Services\UserService()) -> findFirstById($res -> user -> pid);
        if($resPUser['code'] === 0) {
          return $this -> returnSuccessByJson(array_merge(['msg'=>'Success'], $res -> toArray(), ['p_user'=>$resPUser['model']]));
        }
      }

    return $this -> returnSuccessByJson(array_merge(['msg'=>'Success'], $res -> toArray()));
  }
}
